<template>
  <div id="meituan-refund">
    <c-title text="">
      <div class="search-address" slot="left" @click="toCityPage">
        <span>{{ city_name }}</span>
        <van-icon name="arrow-down" size="0.8125rem" />
      </div>
    </c-title>

    <div class="background_box"></div>

    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" v-if="CarouseList && CarouseList.length !== 0">
      <van-swipe-item v-for="item in CarouseList" :key="item.id">
        <img :src="item.pic_url" style="width: 100%;height: 100%;border-radius: 0.625rem;" @click="link(item.url)" />
      </van-swipe-item>
    </van-swipe>
    <div class="ment" v-if="(button_group && button_group.length !== 0)">
      <c-ment :datas="fun.chunk(button_group, 10)" @open_dialog="open_dialog"></c-ment>
    </div>
    <div style="padding: 0 0.75rem;">
      <van-tabs v-if="category_list && category_list.length !== 0" v-model="active" title-active-color="#00001C"
        line-width="0.9375rem" @click="category_handle">
        <van-tab v-for="(item, i) in category_list" :title="item.name" :key="i">
          <div class="category-box">
            <span v-for="(item, i) in category_list_2" :key="item.id" @click="second_category_handle(item.id, i)"
              :class="{ current_category: second_category_index == i }">{{ item.name }}</span>
          </div>
        </van-tab>
      </van-tabs>
    </div>

    <div class="search-box">
      <div class="search">
        <div class="search-input">
          <i class="iconfont icon-sousuo1"></i>
          <input v-model="keyword" type="text" placeholder="请输入关键词搜索"/>
          <button class="search-button" @click="goto()">搜索</button>
        </div>
      </div>
    </div>

    <div class="box" v-for="(item, i) in list" :key="i" @click="detail_handle(item)">
      <div class="box-left">
        <div class="box-left-img">
          <img :src="item.dealBaseInfo.defaultPic" alt="" />
        </div>
        <div class="distance">距离：{{ item.shopInfo.distanceToShow }}</div>
        <div class="make-money" v-if="item.dealBaseInfo.share_reward.amount !== '0.00'">
          <span class="profit">赚</span>
          <span class="price">{{ $i18n.t('money') }}{{ item.dealBaseInfo.share_reward.amount }}</span>
        </div>
      </div>
      <div class="box-right">
        <div class="title">{{ item.dealBaseInfo.dealTitle }}</div>
        <div class="tips">{{ item.shopInfo.brandName }}</div>
        <div class="illustrate" :class="{ 'space-around': item.dealBaseInfo.couponPrice > 0 }">
          <div>
            <div>美团价</div>
            <div class="fz-24">{{ $i18n.t('money') }}{{ item.dealBaseInfo.realPrice }}</div>
          </div>
          <div v-if="item.dealBaseInfo.couponPrice > 0">
            <div>其他立减</div>
            <div class="fz-24">{{ $i18n.t('money') }}{{ (item.dealBaseInfo.couponPrice).toFixed(2) }}</div>
          </div>
        </div>
        <div class="preferential">
          <span style="margin-right: 0.1563rem;">券后价</span>
          <span style="font-weight:bold;">{{ $i18n.t('money') }}</span>
          <div style="display: flex;align-items: center;">
            <span style="font-size: 1.125rem;font-weight: bold;line-height: 1;">{{ item.dealBaseInfo.finalPrice }}</span>
            <span class="discount" v-if="item.dealBaseInfo.discount">{{ item.dealBaseInfo.discount }}</span>
          </div>
        </div>
        <div class="preferential-2">
          <span>门市价</span>
          <span style="text-decoration: line-through;">{{ $i18n.t('money') }}{{ item.dealBaseInfo.marketPrice }}</span>
        </div>
      </div>
    </div>

    <van-empty v-if="list && list.length == 0" description="暂无数据" />

  </div>
</template>

<script>
import { Image as VanImage } from "vant";
import { scrollMixin } from "../../utils/mixin";
import cMent from "./components/menu.vue";
export default {
  mixins: [scrollMixin],
  components: { VanImage, cMent },
  data() {
    return {
      keyword: "",
      choose_index: 1,
      city_name: "请选择",
      SearchValue: "",
      CarouseList: [],
      second_category_index: 0,

      list: [],
      current_page: 1,
      last_page: 1,
      isLoadMore: true,

      cate_id: "",//一级分类id
      second_cate_id: "",//二级分类id

      sort: {
        up: 0,
        down: 0
      },
      category_list: [],
      category_list_2: [],
      button_group: [],

      city_id: "",
      point: {},
      currrent_category_index: 0
    };
  },
  activated() {
    this.init();
    this.getBanner();
    this.buttonGroup()

    let myLoaction = JSON.parse(localStorage.getItem('myLocation'));
    if (localStorage.getItem('meituan_city_id') && this.$route.query.city) {//城市选择器页进入
      let city_id = localStorage.getItem('meituan_city_id');
      this.city_id = city_id;
      this.city_name = this.$route.query.city;
      this.getCategory(city_id);
    } else if (myLoaction) {
      this.point = myLoaction.point;
      this.city_name = myLoaction.city;
      this.get_city_id(myLoaction.city)
    } else {
      this.fun
        .getLocation()
        .then((res) => {
          this.get_city_id(res.city)
          this.point = res.point;
          this.city_name = res.city;
          this.city_id = res.addressComponent.citycode;
        })
        .catch(err => {
          console.log(err);
        });
    }

  },

  methods: {
    init(){
      this.city_id = "";
      this.city_name = "请选择";
      this.isLoadMore = true;
      let myLoaction = JSON.parse(localStorage.getItem('myLocation'));
      if(myLoaction){
        this.point = myLoaction.point;
      }
    },
    get_city_id(name) {
      $http.post('plugin.meituan-group-buy.frontend.good.city', { name }, '').then(res => {
        if (res.result !== 1) {
          return this.$toast(res.msg);
        }
        let datas = res.data;
        let city_id;
        for (let item in datas) {
          city_id = datas[item][0].id;
        }
        this.city_id = city_id;
        window.localStorage.setItem('meituan_city_id', city_id)
        this.getCategory(city_id);
      })
    },
    detail_handle(item) {
      this.$router.push(this.fun.getUrl('meituan_refund_detail', {}, { dealid: item.dealBaseInfo.dealGroupId, cate_id: this.cate_id, lng: this.point.lng, lat: this.point.lat }));
    },
    toCityPage() {
      this.$router.push(this.fun.getUrl("citypickerPage", {}, { plu: "meituan" }))
    },
    goto() {
      if (!this.city_id) {
        return this.$toast("请先选择城市");
      }
      let json = {
        lat: this.point.lat,
        lng: this.point.lng,
        city: this.city_name,
        path: 'home',

      }

      if (this.keyword) {
        json.k = this.keyword;
      }
      console.log(json, 'json')
      localStorage.setItem('meituanCategory', JSON.stringify({ 'cate_id': this.cate_id, 'sId': this.second_cate_id }))
      this.$router.push(this.fun.getUrl('meituan_refund_search', {}, json))
    },
    open_dialog(data) {
      if (data.type == 1 || data.type == 2) {
        if (!this.city_id) {
          return this.$toast("请先选择城市");
        }
        let json = {
          lat: this.point.lat,
          lng: this.point.lng,
          city: this.city_name,
          path: 'home',
          k: data.keyword

        }
        localStorage.setItem('meituanCategory', JSON.stringify({ 'cate_id': data.first_cate_id, 'sId': data.second_cate_id }))
        this.$router.push(this.fun.getUrl('meituan_refund_search', {}, json))
      } else if (data.type == 3) {
        this.link(data.url)
      }
    },
    async buttonGroup() {
      let { data, msg, result } = await $http.get("plugin.meituan-group-buy.frontend.index.button-group");
      if (result == 0) return this.$toast(msg);
      this.button_group = data;
    },
    async getCategory(city_id) {
      let { data, msg, result } = await $http.get("plugin.meituan-group-buy.frontend.good.category", { city_id }, '');
      if (result == 0) {
        return this.$toast(msg)
      }
      this.category_list = data || [];
      this.cate_id = data[this.currrent_category_index].id
      this.getChildrenCategory(this.cate_id);
    },
    getChildrenCategory(first_cate_id) {
      $http.get("plugin.meituan-group-buy.frontend.good.category", { first_cate_id, city_id: this.city_id }, '').then(res => {
        if (res.result !== 1) {
          return this.$toast(res.msg);
        }
        this.category_list_2 = res.data;
        this.second_cate_id = res.data[this.second_category_index].id;
        this.getData();
      })
    },
    category_handle(index) {
      this.getChildrenCategory(this.category_list[index].id);
      this.currrent_category_index = index;
      this.cate_id = this.category_list[index].id;
    },
    link(url) {
      window.open(url, "_blank");
    },
    async getData(page = 1) {
      console.log(this.city_id, "this.city_id====")
      let json = {
        page,
        first_cate_id: this.cate_id,
        second_cate_id: this.second_cate_id,
        geo_info: {}
      }
      if (this.point) {
        json.geo_info.lat = this.point.lat;
        json.geo_info.lng = this.point.lng;
      }
      if (this.city_id) {
        json.geo_info.cityId = this.city_id;
      }
      let { result, msg, data } = await $http.post("plugin.meituan-group-buy.frontend.good.good", json, '');
      if (result == 0) {
        return this.$toast(msg);
      }
      if (json.page == 1) {
        this.list = data.records || [];
      } else if (json.page > 1) {
        this.list = this.list.concat(data.records);
      }
      if (data.good_detail.cservice || data.good_detail.group_code) {
        if (data.good_detail.cservice) {
          localStorage.setItem('meituan_cservice', JSON.stringify(data.good_detail.cservice))
        }
        if (data.good_detail.group_code) {
          localStorage.setItem('meituan_group_code', JSON.stringify(data.good_detail.group_code))
        }
      }

      this.current_page = Number(data.current_page);
      this.last_page = data.last_page;
      this.isLoadMore = true;
    },
    async getBanner() {
      let { result, msg, data } = await $http.get("plugin.meituan-group-buy.frontend.index.rotation");
      if (result == 0) {
        return this.$toast(msg);
      }
      this.CarouseList = data || [];
    },
    getMoreData() {
      if (this.current_page >= this.last_page) {
        return;
      }
      this.isLoadMore = false;
      let page = this.current_page + 1;
      this.getData(page);
    },
    second_category_handle(id, i) {
      this.second_category_index = i;
      this.second_cate_id = id;
      this.getData()
    },
  }
};
</script>

<style lang='scss' scoped>
.background_box {
  position: absolute;
  left: 0;
  right: 0;
  top: 40px;
  z-index: -1;
  height: 8.125rem;
  background-color: #fff;
  background-image: -webkit-gradient(linear, left top, left bottom, from(var(--themeBaseColor)), to(#f5f5f5));
  background-image: -webkit-linear-gradient(var(--themeBaseColor), #f5f5f5);
  background-image: linear-gradient(var(--themeBaseColor), #f5f5f5);
}

.space-around {
  position: relative;
  justify-content: space-around !important;
}

.space-around::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 0.625rem;
  height: 0.0938rem;
  border-radius: 0.125rem;
  background: #F15353;
}

.my-swipe {
  margin: 0.4375rem 0.8438rem;

}

::v-deep .van-tabs__nav {
  background-color: unset;
}

::v-deep .van-tabs--line .van-tabs__wrap {
  height: 36px;
}

::v-deep .van-tab--active {
  font-weight: bold;
}

::v-deep .van-tabs__line {
  background-color: #F15353;
}

.category-box::-webkit-scrollbar {
  display: none;
}

.category-box {
  display: flex;
  align-items: center;
  overflow-y: scroll;
  margin: 0.8438rem 0.875rem;
  min-height: 1.6rem;

  span {
    height: 1.5rem;
    line-height: 1.5rem;
    padding: 0 0.625rem;
    background: #FFFFFF;
    border: 1px solid #FFFFFF;
    border-radius: 0.375rem;
    margin-right: 0.625rem;
    flex-shrink: 0;
    box-sizing: border-box;
  }
}

.box {
  background-color: #fff;
  border-radius: 0.5rem;
  margin: 0.5rem 0.75rem;
  padding: 0.75rem;
  display: flex;

  .fz-24 {
    font-size: 0.75rem;
  }

  .illustrate {
    position: relative;
    height: 2.75rem;
    background: #FFF5F5;
    border-radius: 0.375rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 0.6875rem;
    color: #F15353;
  }

  .illustrate::after {
    position: absolute;
    content: "";
    bottom: -0.3rem;
    left: 0.6563rem;
    background: #FFF5F5;
    width: 1.375rem;
    height: 1.375rem;
    transform: rotate(45deg);
  }

  .preferential {
    margin-top: 0.9375rem;
    display: flex;
    color: #F15353;
    font-size: 0.75rem;
    align-items: baseline;
  }

  .preferential-2 {
    margin-top: 0.3438rem;
    display: flex;
    color: #AAAAB3;
    font-size: 11px;
    align-items: baseline;

  }

  .discount {
    height: 1.0313rem;
    line-height: 1.0313rem;
    padding: 0 0.2188rem;
    box-sizing: border-box;
    border-radius: 0.1875rem;
    border: 1px solid #F15353;
    font-size: 0.625rem;
    margin-left: 0.25rem;
  }

  .box-right {
    margin-left: 0.375rem;
    flex: 1;

    .title {
      text-align: left;
      margin-top: 0.375rem;
      font-size: 0.9375rem;
      font-weight: bold;
      color: #00001C;
    }

    .tips {
      font-size: 0.75rem;
      font-weight: 400;
      color: #6E6E79;
      margin: 0.75rem 0;
      text-align: left;
    }
  }

  .distance {
    color: #6E6E79;
    font-size: 0.75rem;
    margin-top: 0.8438rem;
    margin-bottom: 0.625rem;
    text-align: left;
  }

  .box-left {
    flex-shrink: 0;
  }

  .box-left-img {
    width: 6.25rem;
    height: auto;
  }

  img {
    width: 100%;
    height: 100%;
    border-radius: 0.625rem;
  }

  .make-money {
    max-width: 4.9375rem;
    height: 1.5rem;
    line-height: 1.5rem;
    border-radius: 0.25rem;
    display: flex;
    justify-content: space-between;
    color: #ff7b05;
    border: 1px solid #ff7b05;

    .profit {
      position: relative;
      font-size: 0.75rem;
      padding: 0 0.375rem;
      border-right: 1px dotted #F96D20;
    }

    .price {
      flex: 1;
      text-align: center;
      font-size: 0.8125rem;
      font-weight: bold;
      color: #F96D20;
    }
  }
}

::v-deep .van-nav-bar {
  background-color: var(--themeBaseColor) !important;
}

::v-deep .van-nav-bar {
  border-bottom: none;
}

::v-deep .van-button--default {
  background-color: var(--themeBaseColor) !important;
  color: #fff;
}

::v-deep .van-nav-bar .van-icon {
  color: #fff;
}

.van-icon-arrow-down {
  display: flex;
  align-items: center;
  margin-left: 0.1563rem;
}

#meituan-refund {
  position: relative;

  .search-box {
    // height: 9.3125rem;
    // background: linear-gradient(180deg, #F15353 0%, #F5F5F5 100%);
  }

  .search {
    display: flex;
    align-items: center;
    padding: 0.3125rem 0.75rem;
  }

  .search-input {
    flex: 1;
    display: flex;
    align-items: center;
    height: 2.25rem;
    background: #FFFFFF;
    border-radius: 1.25rem;
    padding: 0.1875rem 0.25rem 0.1875rem 1.0938rem;

    .icon-sousuo1 {
      font-size: 0.875rem;
      margin-right: 0.2188rem;
      color: #999999;
    }

    input {
      flex: 1;
      border: none;
      border-radius: 100px;
      padding-left: 0.1563rem;
    }
  }

  .search-button {
    padding: 0.375rem 0.8125rem;
    background: var(--themeBaseColor);
    font-size: 0.8125rem;
    border-radius: 1.25rem;
    border: none;
    flex-shrink: 0;
    color: #fff;
  }

  .search-address {
    display: flex;
    align-self: center;
    color: #fff;
    font-size: 0.9375rem;
    margin-left: 0.6875rem;
  }

  .ment {
    background: #fff;
    margin: 0.625rem 0.75rem;
    border-radius: 0.5rem;
  }

  .current_category {
    // color: var(--themeBaseColor);
    color: #F15353;
    border: 1px solid #F15353;
    background-color: #F5E4E4;
  }

  .category {
    display: flex;
    padding: 0.75rem;
    overflow-x: auto;

    span {
      margin-right: 1.5625rem;
      font-size: 0.8125rem;
      flex-shrink: 0;
    }
  }

  .category::-webkit-scrollbar {
    display: none;
  }

  .list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
    background: #f1f1f1;
    padding-bottom: 3.125rem;
  }

  .p-4 {
    padding: 0.1rem 0.25rem;
  }

  .item-center {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 0.5rem;
  }

  .img {
    width: 100%;
    height: 10.5938rem;
  }

  .flex {
    display: flex;
  }

  .fs-10 {
    font-size: 0.625rem;
  }

  .between {
    justify-content: space-between;
  }

  .sort {
    position: relative;
  }

  .sort-item {
    position: absolute;
    right: -20px;
    top: 0;
    bottom: 0;
    width: 20px;

    &::after {
      content: "";
      left: 0;
      top: 0;
      position: absolute;
      border: 5px solid #000;
      border-top-color: transparent;
      border-bottom-color: #ccc;
      border-left-color: transparent;
      border-right-color: transparent;
    }
  }

  .item-bottom {
    width: 100%;
    display: flex;
    align-items: center;
    padding: 0.25rem 0.5rem 0.9rem;
  }

  .ml-4 {
    margin-left: 0.25rem;
  }

  .line-through {
    text-decoration: line-through;
  }

  .ml-12 {
    margin-left: 0.75rem;
  }

  .c-999 {
    color: #999;
  }

  .c-FF6238 {
    color: #ff6238;
  }

  .fs-12 {
    font-size: 0.75rem;
  }

  .make-money {
    border-radius: 0.25rem;
    display: flex;
    justify-content: space-between;
    color: #ff7b05;
    border: 1px solid #ff7b05;

    .line {
      position: relative;

      &::after {
        content: "";
        position: absolute;
        top: 0;
        bottom: 0;
        right: 0;
        width: 0.0313rem;
        height: 100%;
        background: #ff7b05;
      }
    }
  }

  .list-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 46%;
    overflow: hidden;
    background: #fff;
    border-radius: 0.5rem;
    overflow: hidden;
    margin: 0.75rem 0.375rem 0;
  }

  .tik-tok-text {
    color: rgb(255, 255, 255);
    background: rgb(0, 0, 0);
    padding: 0 5px;
    border-radius: 5px;
    margin-right: 5px;
    font-size: 0.75rem;
  }



  .buy {
    color: #999;
    font-size: 0.75rem;
  }




  .tab {
    display: flex;
    justify-content: space-evenly;
    padding-top: 10px;
    padding-bottom: 12px;
  }

  .van-search {
    flex: 1;
  }

  .top-right {
    display: flex;
    align-items: center;
    flex-direction: column;
    margin: 10px 15px;
    padding-top: 10px;
  }

  .top {
    display: flex;
    background: #fff;
    align-items: center;
  }
}
</style>